
;;====================================================================
;; Routine to generate the IDL save file bessel_parallax.sav
;;   containing the IDL strucutre parallax.  This routine takes the
;;   latest BeSSeL results (in the file bessel_results.dat from
;;   http://www.mpifr-bonn.mpg.de/staff/abrunthaler/BeSSeL/prod_para.shtml) 
;;   and converts it to a more useful form.

s = read_bgps_csv(/ver)
bfn = './ancillary/bessel_results.dat'
n = FILE_LINES(bfn)

openr,lun,bfn,/get_lun

parallax = replicate( create_struct( 'Name','','l',0.,'b',0.,$
                                     'pi',0.,'sig_pi',0.,$
                                     'vlsr',0.,'sig_v',0.,'cnum',0L), n)

FOR i=0L, n-1 DO BEGIN
   line = ''
   readf,lun,line
   
   parallax[i].name   = strtrim(strmid(line,0,12),2)
   parallax[i].l      = float(strmid(line,12,8))
   parallax[i].b      = float(strmid(line,21,8))
   parallax[i].pi     = float(strmid(line,31,6))
   parallax[i].sig_pi = float(strmid(line,39,6))
   parallax[i].vlsr   = float(strmid(line,47,5))
   parallax[i].sig_v  = float(strmid(line,54,2))
   
   p = parallax[i]
   gcirc,2,p.l,p.b,s.glon_peak,s.glat_peak,dis
   
   dmin = MIN(dis, mind, /NAN)
   IF dmin LE 100. THEN parallax[i].cnum = s[mind].cnum
   
   ;; spawn,'cp ./local/hv_slides/bgps'+$
   ;;       string(parallax[i].cnum,format="(I04)")+$
   ;;       '.eps /Volumes/TPEP_MW2011/BGPS/.'
   
   print,parallax[i]
ENDFOR

save,parallax,filename='./ancillary/bessel_parallax.sav',/ver
END
